//
//  ViewController.m
//  UIWebView
//
//  Created by keisun on 2017/6/19.
//  Copyright © 2017年 keisun. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()<UIWebViewDelegate>

@property(nonatomic,strong)UIWebView *webView;
@property(nonatomic,strong)UIBarButtonItem *backItem;
@property(nonatomic,strong)UIBarButtonItem *forwardItem;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    

    [self setupWebView];
}

-(void)setupWebView{
    UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height-44)];
    webView.delegate = self;
    [self.view addSubview:webView];
    //    [webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"https://www.baidu.com"]]];
    //    [webView loadHTMLString:@"<html><body><div style=\"color: red; font-size:10px; border:1px solid blue;\">哈哈哈哈哈</div></body></html>" baseURL:nil];
    [webView loadRequest:[NSURLRequest requestWithURL:[[NSBundle mainBundle] URLForResource:@"test" withExtension:@"html"]]];
    self.webView = webView;
    
    //检测各种特殊字符串：电话，网址
    webView.dataDetectorTypes = UIDataDetectorTypeAll;
    webView.scrollView.contentInset = UIEdgeInsetsMake(20, 0, 0, 0);
    
    //底部工具栏
    UIToolbar *tool = [[UIToolbar alloc] initWithFrame:CGRectMake(0,self.view.frame.size.height-44, self.view.frame.size.width, 44)];
    [self.view addSubview:tool];
    
    UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithTitle:@"后退" style:UIBarButtonItemStylePlain target:self action:@selector(backClick:)];
    self.backItem = backItem;
    
    UIBarButtonItem *forwardItem = [[UIBarButtonItem alloc] initWithTitle:@"前进" style:UIBarButtonItemStylePlain target:self action:@selector(forwardClick:)];
    self.forwardItem = forwardItem;
    
    UIBarButtonItem *refreshItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh target:self action:@selector(refreshClick:)];
    
    tool.items = @[backItem,forwardItem,refreshItem];
}

//页面后退
-(void)backClick:(UIBarButtonItem *)item{
    [self.webView goBack];
}

//页面前进
-(void)forwardClick:(UIBarButtonItem *)item{
    [self.webView goForward];
}

//页面刷新
-(void)refreshClick:(UIBarButtonItem *)item{
    [self.webView reload];
}



#pragma mark - UIWebViewDelegate
-(void)webViewDidStartLoad:(UIWebView *)webView{
    NSLog(@"%s",__func__);
}

-(void)webViewDidFinishLoad:(UIWebView *)webView{
    NSLog(@"%s",__func__);
    self.backItem.enabled = self.webView.canGoBack;
    self.forwardItem.enabled = self.webView.canGoForward;
}

-(void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error{
    NSLog(@"%s",__func__);
    self.backItem.enabled = self.webView.canGoBack;         //能否后退
    self.forwardItem.enabled = self.webView.canGoForward;   //能否前进
}

/**
 每当webView发送一个请求之前（但凡重定向URL），都会调用这个方法。
 返回YES，允许加载这个请求
 返回NO，禁止加载这个请求
 */
-(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{
    
    NSLog(@"url:%@",request.URL);
    if ([request.URL.absoluteString containsString:@"life"]) {
        return NO;
    }
    return YES;
}
@end
